package user

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
	"time"
)

func (c *Controller) MemberRegister() {
	organizationID, err := c.GetInt("organization_id")
	if err != nil {
		c.ResponseFailed(201, "organization_id 不合法")
		return
	}

	if value := db.DB.Where("id = ?", organizationID).First(&models.Organization{}); value.Error != nil {
		c.ResponseFailed(202, "无此社团")
		return
	}

	organizationUser := models.OrganizationUser{
		OrganizationID: uint(organizationID),
		UserID:         c.UserID,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
		Status:         models.OrganizationUserRegister,
	}

	if value := db.DB.Create(&organizationUser); value.Error != nil {
		c.ResponseFailed(203, "申请社团失败")
		return
	}

	c.ResponseSuccess()
}

func (c *Controller) MyMembers() {
	var members []models.OrganizationUser
	db.DB.Preload("Organization").Where("user_id = ?", c.UserID).Find(&members)
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["members"] = members
	c.Data["json"] = result
	c.ServeJSON()
}
